﻿Global.namespace("System.Web.Page");

System.Web.Page.SendMessage=function(config){
    config.templateFile="SendMessage.htm?t="+ Global.random();
    this.receiverId=config.receiverId;
    this.isSendGift=config.isSendGift;
    this.receiverName=config.receiverName;
    
    System.Web.Page.SendMessage.superclass.constructor.call(this,config);
}
Global.extend(System.Web.Page.SendMessage,System.Core.UI.BasePage,{
    onLoading:function(){
        Global.showLoadingMask({
            wrapper:'#'+this.id,
            id:'sendmessage_Loading'
        });
    },
    isSendGift:false,
    gifts:null,
    onRendered:function(){
        var ctr=this;
        $('#receiver').html(ctr.receiverName);
        
        if(this.isSendGift===true){
            $('#gift').addClass('MinLoadingMask');
            $('.GiftList').removeClass('None');
            
            var url= Config.rootServiceURL + "inlineservice.axd?action=getallgift&t"+Global.random();
            System.Core.Handler.RequestHandler.get(url,{},function(response){
                var data=StringHandler.format('({0})',System.Core.Handler.RequestHandler.getResponseData(response));
                ctr.bindGift(eval(data));
            });
        }
        // hide lopading mask
        Global.hideLoadingMask('sendmessage_Loading');
        
        $('#btnSend').click(function(){
            if(!ctr.validateInput()){ return;}
            ctr.submit();
        });
    },
    bindGift:function(gifts){
        var repeater=new System.Core.UI.Repeater({
            template:"<ul id='{0}' />",
            wrapper:'#gift',
            dataStore:gifts,
            headerTemplate:function(){
                return "";
            },
            itemTemplate:function(item){
                var html='<li><input type="checkbox" id="gift" class="GiftChk" value="{0}" /><img src="{1}Gifts/Fplant_icon{0}.png" /><span>{2} ({3}$)</span></li>';
                return StringHandler.format(
                    html,
                    item.GiftId,
                    Config.imageUrl,
                    item.Name,
                    item.Price
                );
            },
            noItemTemplate:function(){
                return "<li>No gift was found</li>"
            },
            footerTemplate:function(){
                return "";
            },
            onRendered:function(){
                $('#gift').removeClass('MinLoadingMask');
            }
        });
        repeater.render();
        
    },
    validateInput:function(){
        if($.trim($('#txtContent').val())===''){
            $('#txtContent').addClass("ValidateError");
            return false;
        }else{
            $('#txtContent').removeClass("ValidateError");
        }
        
        var gifts=$('.GiftChk:checked');
        if(this.isSendGift && gifts.length<=0){
            $('.Gift').addClass('ValidateError');
            alert('Add class');
            return false;
        }else{
            $('.Gift').removeClass('ValidateError');
        }
        
        return true;
    },
    submit:function(){
        var ctr=this;
        var content=$('#txtContent').val();
        var gifts=$('.GiftChk:checked');
        var giftId='';
        // send gift to friend
        
        if(this.isSendGift){
            for(var index=0; index<gifts.length; index++){
                giftId+=gifts[index].value+(index<gifts.length-1?",":"");
            }
            //content=StringHandler.format(System.Web.Lang.Message.SendGift,'Thanh Tu',content);
        }
        System.Web.Handler.MessageHandler.sendInvitation(
            this.receiverId,
            content,
            function(){
                if(!ctr.isSendGift){
                    var masterPage=ctr.parent;
                    masterPage.loadControl(System.Web.Page.Home, { wrapper: '.MasterPageWrapper',autoRender:true  });
                    return;
                }
                System.Web.Handler.MessageHandler.sendGifts({
                    receiverId:ctr.receiverId,
                    gift:giftId,
                    message:content,
                    callback:function(data){
                        var masterPage=ctr.parent;
                        masterPage.loadControl(System.Web.Page.Home, { wrapper: '.MasterPageWrapper',autoRender:true  });
                    }
                });
            }
        );
    }
});